package com.by.servlet;

import com.by.dao.AdminDao;
import com.by.entity.Admin;
import com.by.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/adminlogin")
public class AdminLoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String aname = request.getParameter("aname");
        String apwd = request.getParameter("apwd");
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        Admin admin = new Admin(aname, apwd);
        AdminDao adminDao = session.getMapper(AdminDao.class);

        Admin a = adminDao.adminLogin(admin);

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if (a != null) {
            out.println("登录成功");
        } else {
            out.println("<script>location.href='adminLogin.html'</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
